<%@ page import="domain.Product" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="dao.ProductDao" %>
<%--
  Created by IntelliJ IDEA.
  User: ThinkPad
  Date: 2021/12/23
  Time: 11:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
    <title>后台管理系统</title>
</head>
<body>
<%@include file="headForRoot.jsp"%>
<script>
    function delete_confirm(pid, pname) {
        var conf = confirm("确定要删除吗？“" + pname + "”将彻底从数据库中删除（无法撤销）！");
        if (conf)window.location="delete.jsp?delete="+pid;
    }
</script>
<div style="text-align: right;margin-right: 100px">
    <a href="${pageContext.request.contextPath}/download.jsp">销售统计报表下载</a><br />
    <a href="${pageContext.request.contextPath}/log.jsp">浏览客户购买日志</a>
    <%-- 实现搜索功能 --%>
    <form method="post" action="${pageContext.request.contextPath}/manage.jsp">
        <label><input type="text" name="search"></label>
        <input type="submit" value="搜索商品">
    </form>
</div>
<%
    String search = request.getParameter("search");
    if (search !=null){
        request.getSession().setAttribute("pname", search);
        if (search.equals("")){
            request.getSession().removeAttribute("pname");
        }
    }
    String pname = (String) request.getSession().getAttribute("pname");
    if (pname!=null){
        out.println("<div style=\"margin-left: 120px;color: red;\">" +
                "<b>温馨提示</b>：您搜索的“"+pname+"”翻页后仍有效，若想清空您的搜索关键字，请直接点击”搜索“按钮</div>");
    }
%>
<br /><br />
<%-- 实现产品浏览功能 --%>
<div style="margin-left: 120px;color: indianred;"><b>产品浏览及编辑：</b></div>
<br />
<a href="append.jsp"><div style="text-align:right;margin-right: 200px;color: blue"><b>添加商品</b></div></a>
<%
    String pageString = request.getParameter("page");
    ArrayList<Product> products = new ArrayList<>();
    ProductDao dao = new ProductDao();
    int pageIndex = 1;
    int totalPage = dao.getTotalPage(pname);
    try {
        // 页面的默认值是1
        pageIndex = pageString==null?1:Integer.parseInt(pageString);
        if (pageIndex<=0){
%>
<script>
    alert("已经是第一页了哦！");
    window.location="manage.jsp?page=1";
</script>
<%
    }
    if (pageIndex>totalPage){
%>
<script>
    alert("已经是最后一页了哦！");
    window.location="manage.jsp?page="+<%=totalPage%>;
</script>
<%
        }
        products = dao.browse(pageIndex,pname);
    } catch (NumberFormatException e) {
        e.printStackTrace();
    }
%>
<%-- 利用JSTL标签输出产品的值 --%>
<br />
<c:forEach var="prod" items="<%=products%>">
    <table style="border: 3px solid sandybrown;margin-left: 15%;height: 220px;width: 70%">
        <tr>
            <td rowspan="4" style="width: 220px"><img src="${prod.img}" alt="" height="200px" width="200px"></td>
            <td style="margin-left: 20px"><b>产品编号：</b>${prod.id}</td>
            <td style="margin-left: 20px"><b>产品名称：</b>${prod.name}</td>
        </tr>
        <tr>
            <td colspan="2" style="margin-left: 20px" height="50%"><b>产品描述：</b><br />
                <div style="font-size: small;color:grey">${prod.description}</div></td>
        </tr>
        <tr>
            <td style="margin-left: 20px"><b>产品库存：</b>${prod.count}</td>
            <td style="margin-left: 20px"><b>产品价格：</b>${prod.price}</td>
        </tr>
        <tr>
            <td>
                <a href="modify.jsp?modify=${prod.id}"><div style="text-align: right;color: blue"><b>编辑</b></div></a>
            </td>
            <td style="text-align: right">
                <input type="button" onclick="delete_confirm('${prod.id}','${prod.name}')" value="删除"/>
            </td>
        </tr>
    </table>
    <br />
</c:forEach>
<br /><br />
<%-- 页码切换 --%>
<div style="text-align: center">
    <a href="${pageContext.request.contextPath}/manage.jsp?page=<%=pageIndex-1%>">上一页</a>
    &nbsp;&nbsp;第<%=pageIndex%>页，共<%=totalPage%>页&nbsp;&nbsp;
    <a href="${pageContext.request.contextPath}/manage.jsp?page=<%=pageIndex+1%>">下一页</a>
</div>
<%@include file="tail.jsp"%>
</body>
</html>
